import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:study_demo/base/BaseController.dart';

import '../../util/utils.dart';

/// json转实体
class JsonToModelController extends BaseController {
  final result = ''.obs;

  TextEditingController _textEditingController = TextEditingController();

  TextEditingController get textEditingController => _textEditingController;

  TextEditingController _resultTextEditingController = TextEditingController();

  TextEditingController get resultTextEditingController =>
      _resultTextEditingController;

  TextEditingController _classNameTextEditingController =
      TextEditingController();

  TextEditingController get classNameTextEditingController =>
      _classNameTextEditingController;

  transform() {
    String attributeInfo = '';
    String jsonData = _textEditingController.text;
    if (isValidJson(jsonData)) {
      // print('jsonData:$jsonData');
      List<dynamic> jsonArray = jsonDecode(jsonData);
      if (jsonArray.isNotEmpty) {
        Map<String, dynamic> jsonObject = jsonArray.first;
        // 遍历Map获取所有键值对
        jsonObject.forEach((key, value) {
          print('Key: $key');
          String valueType;
          if (value is int)
            valueType = 'int?';
          else if (value is double)
            valueType = 'double?';
          else if (value is String)
            valueType = 'String?';
          else if (value is bool)
            valueType = 'bool?';
          else if (value is List)
            valueType = 'List?';
          else if (value is Map)
            valueType = 'Map?';
          else
            valueType = 'Unknown';
          if (key.contains('_')) {}
          attributeInfo +=
              (key.contains('_') ? '@JsonKey(name:"$key")\n' : '') +
                  valueType +
                  ' ' +
                  (key.contains('_') ? key.replaceAll('_', '') : key) +
                  ';\n';
        });
        if (!_classNameTextEditingController.text.isAlphabetOnly) {
          toast('类名格式不正确');
          return;
        }
        String className =
            firstWordToUpper(_classNameTextEditingController.text);
        print('className:$className');
        result.value =
            '@JsonSerializable()\nclass $className extends Object {\n $attributeInfo\n$className();\n\n' +
                'factory $className.fromJson(Map<String, dynamic> srcJson) =>'
                    '\_\$$className\FromJson(srcJson);\n\nMap<String?, dynamic> toJson() => _\$$className\ToJson(this);\n}';

        _resultTextEditingController.text = result.value;
      } else {
        print('json数组为空');
      }
    } else {
      toast('当前内容不是json格式');
    }
  }

  clearZy() {
    if (_textEditingController.text.isNotEmpty &&
        _textEditingController.text.contains('\\')) {
      _textEditingController.text =
          _textEditingController.text.replaceAll('\\', '');
    }
  }

  copyCode() {
    copyToClipboard(_resultTextEditingController.text);
  }

  clear() {
    _textEditingController.clear();
  }

  Future<void> runCommand() async {
    try {
      // if (Platform.isWindows) {
      //   final result = await Process.run('run_build.bat', [], runInShell: true);
      //   if (result.exitCode == 0) {
      //     print(result.stdout);
      //   } else {
      //     print('Error executing dart command: ${result.stderr}');
      //   }
      // } else {
      final result = await Process.run('./run_build.sh', [], runInShell: true);
      if (result.exitCode == 0) {
        print(result.stdout);
      } else {
        print('Error executing dart command: ${result.stderr}');
      }
    } on Exception catch (e) {
      print('Failed to run build script: $e');
    }
  }
}
